<?php
class Picture_model extends CI_Model
{
	public function __construct()
	{
		$this->load->library('session');
		$this->load->database();
	}
	public function getPicturesByUser()
	{
		$sql = 'SELECT pic_name FROM '.CI_Model::$picture.' WHERE user_id = ?;';
		$result = $this->db->query($sql,array($_SESSION['id']))->result_array();
		return count($result) > 0 ? $result: array();
	}
	public function getPictureById($id)
	{
		$sql = 'SELECT pic_name FROM '.CI_Model::$picture.' WHERE auto_id = ?;';
		$result = $this->db->query($sql,array($id))->row_array();
		return isset($result['pic_name'])?($result['pic_name']): null;
	}
	
	public function insertPicture($name)
	{
		$sql = 'INSERT INTO '.CI_Model::$picture.' (auto_id,pic_name,user_id,type,create_time) VALUES (null,?,?,?,?);';
		return $this->db->query($sql,array($name,$_SESSION['id'],0,date('Y-m-d H:i:s')));
	}
	
	public function getIdByName($name)
	{
		$sql = 'SELECT auto_id FROM '.CI_Model::$picture.' WHERE pic_name = ? ;';
		$result = $this->db->query($sql,array($name))->row_array();
		return isset($result['auto_id']) ? $result['auto_id'] : 0;
	}
}